local daiyan = fk.CreateSkill {

  name = "joy__daiyan",

  tags = {  },

}



daiyan:addEffect(fk.EventPhaseStart, {
  name = "joy__daiyan",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(daiyan.name) and player.phase == Player.Finish
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("joy__daiyan_record") ~= 0 then
      local to = room:getPlayerById(player:getMark("joy__daiyan_record"))
      room:setPlayerMark(player, "joy__daiyan_record", 0)
      if not to.dead then
        room:setPlayerMark(to, "@@joy__daiyan-tmp", 1)
      end
    end
    self:doCost(event, target, player, data)
    for _, p in ipairs(room.players) do
      room:setPlayerMark(p, "@@joy__daiyan-tmp", 0)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(player.room.alive_players, Util.IdMapper),
      1, 1, "#joy__daiyan-choose", daiyan.name, true)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "joy__daiyan_record", event:getCostData(self))
    local to = room:getPlayerById(event:getCostData(self))
    local card = room:getCardsFromPileByRule(".|.|heart|.|.|basic")
    if #card > 0 then
      room:moveCards({
        ids = card,
        to = to.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = daiyan.name,
        moveVisible = true,
      })
    end
    if not player.dead and to:getMark("@@joy__daiyan-tmp") == 0 then
      player:drawCards(1,daiyan.name)
    end
  end,
})

return daiyan